/**   
 * @Title: QuestionServiceImplementation.java 
 * @Package cn.edu.cdtu.healthserver.service.impl 
 * @Description: TODO 
 * @author  
 * @date 
 * @version V0.0   
 */
package cn.edu.cdtu.healthserver.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.edu.cdtu.healthserver.dao.QuestionDao;
import cn.edu.cdtu.healthserver.dao.impl.QuestionDaoImplementation;
import cn.edu.cdtu.healthserver.domain.Question;
import cn.edu.cdtu.healthserver.domain.Test;
import cn.edu.cdtu.healthserver.service.QuestionService;

/** 
 * @ClassName: QuestionServiceImplementation 
 * @Description: TODO
 * @author 
 * @date 
 *  
 */
public class QuestionServiceImplementation implements QuestionService {
	
	private QuestionDao questionDao = new QuestionDaoImplementation();

	/* (non-Javadoc)
	 * <p>Title: getQuestions</p> 
	 * <p>Description: </p> 
	 * @param test
	 * @param listAvailable
	 * @return 
	 * @see cn.edu.scu.healthserver.service.QuestionService#getQuestions(cn.edu.scu.healthserver.domain.Test, boolean)
	 */
	@Override
	public List<Question> getQuestions(Test test, boolean listAvailable) {
		// TODO Auto-generated method stub
		List<Question> questions = questionDao.query(test);
		if(listAvailable){
			return questions;
		}
		List<Question> removeList = new ArrayList<Question>();
		for (Question question : questions) {
			if(0==question.getAvailable()){
				removeList.add(question);
			}
		}
		for (Question question : removeList) {
			questions.remove(question);
		}
		return questions;
	}

	/* (non-Javadoc)
	 * <p>Title: addQuestion</p> 
	 * <p>Description: </p> 
	 * @param test
	 * @param question
	 * @return 
	 * @see cn.edu.scu.healthserver.service.QuestionService#addQuestion(cn.edu.scu.healthserver.domain.Test, cn.edu.scu.healthserver.domain.Question)
	 */
	@Override
	public String addQuestion(Test test, Question question) {
		// TODO Auto-generated method stub
		String resultString = null;
		question.setTest_id(test.getId());
		if(questionDao.add(question)){
			resultString = "添加题目成功！";
		}else {
			resultString = "添加题目失败！";
		}
		return resultString;
	}

	/* (non-Javadoc)
	 * <p>Title: updateQuestion</p> 
	 * <p>Description: </p> 
	 * @param question
	 * @return 
	 * @see cn.edu.scu.healthserver.service.QuestionService#updateQuestion(cn.edu.scu.healthserver.domain.Question)
	 */
	@Override
	public String updateQuestion(Question question) {
		// TODO Auto-generated method stub
		String resultString = null;
		if(questionDao.update(question)){
			resultString = "更新题目成功！";
		}else {
			resultString = "更新题目失败！";
		}
		return resultString;
	}

	/* (non-Javadoc)
	 * <p>Title: setAvailable</p> 
	 * <p>Description: </p> 
	 * @param question
	 * @param available
	 * @return 
	 * @see cn.edu.scu.healthserver.service.QuestionService#setAvailable(cn.edu.scu.healthserver.domain.Question, boolean)
	 */
	@Override
	public String setAvailable(Question question, boolean available) {
		// TODO Auto-generated method stub
		String resultString = null;
		if(available){
			question.setAvailable(1);
			if(questionDao.update(question)){
				resultString = "设置题目有效成功！";
			}else {
				resultString = "设置题目有效失败！";
			}
		}else {
			question.setAvailable(0);
			if(questionDao.update(question)){
				resultString = "设置题目失效成功！";
			}else {
				resultString = "设置题目失效失败！";
			}
		}
		return resultString;
	}

}
